package com.haoting.mamba.ucenter.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoting.common.utils.JwtUtils;
import com.haoting.common.utils.MD5;
import com.haoting.mamba.ucenter.entity.vo.LoginVo;
import com.haoting.mamba.ucenter.entity.vo.RegistVo;
import com.haoting.mamba.ucenter.feign.NewsFeignService;
import com.haoting.mamba.ucenter.util.EmailUtil;
import com.haoting.mamba.ucenter.util.VerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.haoting.mamba.ucenter.entity.UserInformationEntity;
import com.haoting.mamba.ucenter.service.UserInformationService;
import com.haoting.common.utils.PageUtils;
import com.haoting.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author tanghao
 * @email 767417609@qq.com
 * @date 2021-03-14 22:56:07
 */
@RestController
@RequestMapping("ucenter/userinformation")
public class UserInformationController {


    @Autowired
    private UserInformationService userInformationService;

    @Autowired
    NewsFeignService newsFeignService;


    @GetMapping("/news/{id}")
    public R test(@PathVariable("id") String id){
        R ne = newsFeignService.info(id);

        return R.ok().put("news",ne.get("news"));
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userInformationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		UserInformationEntity userInformation = userInformationService.getById(id);

        return R.ok().put("userInformation", userInformation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserInformationEntity userInformation){
        userInformation.setPassword(MD5.encrypt(userInformation.getPassword()));
		userInformationService.save(userInformation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserInformationEntity userInformation){
        userInformation.setPassword(MD5.encrypt(userInformation.getPassword()));
		userInformationService.updateById(userInformation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		userInformationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/sendCode/{email}")
    public R sendCode(@PathVariable("email") String email){
        QueryWrapper<UserInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        if (userInformationService.getOne(wrapper)!=null){
            return R.ok().put("result",1);
        }else {
            userInformationService.sendCode(email);
            return R.ok().put("result",0);
        }


    }
    @GetMapping("/sendfindCode/{email}")
    public R sendfindCode(@PathVariable("email") String email){
        QueryWrapper<UserInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        if (userInformationService.getOne(wrapper)!=null){
            userInformationService.sendCode(email);
            return R.ok().put("result",0);
        }else {
            return R.ok().put("result",1);
        }


    }
    @GetMapping("/checkCode/{email}/{code}")
    public R checkCode(@PathVariable("email") String email,@PathVariable("code") String code){
        System.out.println(email+"--"+code);
        String code2 = userInformationService.checkCode(email);
        System.out.println(code2);
        if(code.equals(code2)){
            return R.ok().put("result", 1);
        }else{
            return R.ok().put("result", 2);
        }

    }
    @GetMapping("/getusername/{email}")
    public R getusername(@PathVariable("email") String email){
        QueryWrapper<UserInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        UserInformationEntity one = userInformationService.getOne(wrapper);
        return R.ok().put("username",one.getUsername());
    }
    @GetMapping("/checkUsername/{username}")
    public R checkUsername(@PathVariable("username") String username){

        QueryWrapper<UserInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        if (userInformationService.getOne(wrapper)!=null){
            return R.ok().put("result",1);
        }else {
            return R.ok().put("result",0);
        }

    }
    @GetMapping("/checkemail/{email}")
    public R checkemail(@PathVariable("email") String email){

        QueryWrapper<UserInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        if (userInformationService.getOne(wrapper)!=null){
            return R.ok().put("result",1);
        }else {
            return R.ok().put("result",0);
        }

    }
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        String token = userInformationService.login(loginVo);

        return R.ok().put("token",token);
    }

    @PostMapping("/regist")
    public R checkemail(@RequestBody RegistVo registVo){

        String token = userInformationService.register(registVo);
        return R.ok().put("token",token);

    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        return R.ok().put("userInfo",userInformationService.getById(memberId));
    }

    @GetMapping("updatepassword/{email}/{password}")
    public R updatepassword(@PathVariable("email") String email,@PathVariable("password") String password){
        QueryWrapper<UserInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        UserInformationEntity one = userInformationService.getOne(wrapper);
        one.setPassword(MD5.encrypt(password));
        userInformationService.update(one,wrapper);
        return R.ok();
    }


}
